/**
 * 阅读记录模块类型定义
 * 基于后端API文档：我的模块API.md
 */

// ==================== 基础类型 ====================

/**
 * 阅读模式
 */
export type ReadingMode = 'image' | 'text'

/**
 * 阅读记录项
 */
export interface ReadingRecord {
  id: number
  bookId: number
  bookTitle: string
  bookCover: string
  isCompleted: boolean
  readingProgress: number // 0-100
  readingTime: number // 秒
  readingMode: ReadingMode
  firstReadAt: string
  lastReadAt: string
  completedAt?: string
}

/**
 * 阅读历史响应项
 */
export interface ReadingHistoryRespVO {
  id: number
  bookId: number
  bookTitle: string
  bookCover: string
  bookLanguage?: string
  bookSetId?: number
  bookSetTitle?: string
  isCompleted: boolean
  readingProgress: number
  readingTime: number
  lastReadAt: string
  completedAt?: string
}

/**
 * 阅读统计数据
 */
export interface ReadingStatsRespVO {
  totalBooks: number
  todayBooks: number
  weekBooks: number
  monthBooks: number
  totalReadingTime: number
  todayReadingTime: number
  averageReadingTime?: number
  consecutiveDays?: number
  maxConsecutiveDays?: number
  monthlyStats?: MonthlyStatsVO[]
}

/**
 * 月度统计数据
 */
export interface MonthlyStatsVO {
  month: string
  bookCount: number
  readingTime: number
}

/**
 * 今日阅读状态
 */
export interface ReadingStatusRespVO {
  hasReadToday: boolean
  todayCount: number
  todayTime: number
  lastReadBook?: {
    id: number
    title: string
    cover: string
    readAt: string
  }
}

/**
 * 今日阅读记录响应
 */
export interface TodayReadingRespVO {
  readBookCount: number
  bookList: TodayBookVO[]
}

/**
 * 今日阅读绘本项
 */
export interface TodayBookVO {
  id: number
  title: string
  cover: string
  readAt: string
  readingProgress: number
  isCompleted: boolean
}

// ==================== API响应类型 ====================

/**
 * 分页响应基础类型
 */
export interface PageResponse<T> {
  total: number
  list: T[]
}

/**
 * 阅读记录列表响应
 */
export interface ReadingHistoryPageRespVO extends PageResponse<ReadingHistoryRespVO> {}

// ==================== 请求类型 ====================

/**
 * 分页请求参数
 */
export interface PageParams {
  page: number
  size: number
}

/**
 * 获取阅读记录请求参数
 */
export interface GetReadingHistoryParams extends PageParams {
  lang?: string // 语言类型过滤
}

// ==================== API响应包装类型 ====================

/**
 * 通用API响应格式
 */
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
}

/**
 * 获取阅读记录列表API响应
 */
export type GetReadingHistoryResponse = ApiResponse<ReadingHistoryPageRespVO>

/**
 * 获取阅读统计API响应
 */
export type GetReadingStatsResponse = ApiResponse<ReadingStatsRespVO>

/**
 * 获取今日阅读状态API响应
 */
export type GetReadingStatusResponse = ApiResponse<ReadingStatusRespVO>

/**
 * 删除阅读记录API响应
 */
export type DeleteReadingRecordResponse = ApiResponse<void>

/**
 * 清空阅读记录API响应
 */
export type ClearReadingRecordsResponse = ApiResponse<void>

// ==================== 错误码类型 ====================

/**
 * 阅读记录模块错误码
 */
export enum ReadingRecordsErrorCode {
  RECORD_NOT_FOUND = 50006,
}

/**
 * 错误信息映射
 */
export const ReadingRecordsErrorMessages: Record<ReadingRecordsErrorCode, string> = {
  [ReadingRecordsErrorCode.RECORD_NOT_FOUND]: '阅读记录不存在',
}

// ==================== 组件Props类型 ====================

/**
 * 阅读统计组件Props
 */
export interface ReadingStatsProps {
  stats: ReadingStatsRespVO
  loading?: boolean
}

/**
 * 阅读记录列表组件Props
 */
export interface ReadingHistoryListProps {
  records: ReadingHistoryRespVO[]
  loading?: boolean
  hasMore?: boolean
  onLoadMore?: () => void
  onDelete?: (record: ReadingHistoryRespVO) => void
  onItemClick?: (record: ReadingHistoryRespVO) => void
}

/**
 * 阅读记录项组件Props
 */
export interface ReadingRecordItemProps {
  record: ReadingHistoryRespVO
  onDelete?: (record: ReadingHistoryRespVO) => void
  onClick?: (record: ReadingHistoryRespVO) => void
}

/**
 * 阅读记录管理组件Props
 */
export interface ReadingRecordsManageProps {
  onClearAll?: () => void
  onRefresh?: () => void
  totalCount?: number
}

// ==================== 状态管理类型 ====================

/**
 * 阅读记录模块状态
 */
export interface ReadingRecordsState {
  records: ReadingHistoryRespVO[]
  stats: ReadingStatsRespVO | null
  todayStatus: ReadingStatusRespVO | null
  loading: boolean
  hasMore: boolean
  total: number
  error: string | null
}

/**
 * 阅读记录模块Actions
 */
export interface ReadingRecordsActions {
  getReadingHistory: (params: GetReadingHistoryParams) => Promise<void>
  getReadingStats: () => Promise<void>
  getTodayStatus: () => Promise<void>
  deleteRecord: (recordId: number) => Promise<void>
  clearAllRecords: () => Promise<void>
  clearError: () => void
  resetRecords: () => void
}

// ==================== 工具类型 ====================

/**
 * 阅读时间格式化选项
 */
export interface ReadingTimeFormatOptions {
  unit?: 'second' | 'minute' | 'hour'
  showUnit?: boolean
  precision?: number
}

/**
 * 默认时间格式化选项
 */
export const DEFAULT_TIME_FORMAT: ReadingTimeFormatOptions = {
  unit: 'minute',
  showUnit: true,
  precision: 1,
}

/**
 * 阅读进度颜色配置
 */
export interface ProgressColorConfig {
  low: string // 0-30%
  medium: string // 31-70%
  high: string // 71-99%
  completed: string // 100%
}

/**
 * 默认进度颜色配置
 */
export const DEFAULT_PROGRESS_COLORS: ProgressColorConfig = {
  low: '#ff6b6b',
  medium: '#ffa726',
  high: '#66bb6a',
  completed: '#4caf50',
}

/**
 * 阅读统计卡片配置
 */
export interface StatsCardConfig {
  key: keyof ReadingStatsRespVO
  label: string
  icon: string
  color: string
  unit?: string
}

/**
 * 默认统计卡片配置
 */
export const DEFAULT_STATS_CARDS: StatsCardConfig[] = [
  {
    key: 'todayCount',
    label: '今日阅读',
    icon: 'calendar',
    color: '#ff6b6b',
    unit: '本',
  },
  {
    key: 'weekCount',
    label: '本周阅读',
    icon: 'time',
    color: '#4ecdc4',
    unit: '本',
  },
  {
    key: 'readingDays',
    label: '阅读天数',
    icon: 'checkmark-circle',
    color: '#45b7d1',
    unit: '天',
  },
  {
    key: 'joinDays',
    label: '加入天数',
    icon: 'heart',
    color: '#9c27b0',
    unit: '天',
  },
]

/**
 * 分页默认配置
 */
export const DEFAULT_PAGE_CONFIG = {
  page: 1,
  size: 20,
}

/**
 * 阅读记录排序选项
 */
export type ReadingRecordSortType = 'time_desc' | 'time_asc' | 'progress_desc' | 'progress_asc'

/**
 * 排序配置
 */
export interface SortConfig {
  key: ReadingRecordSortType
  label: string
  icon: string
}

/**
 * 默认排序配置
 */
export const DEFAULT_SORT_OPTIONS: SortConfig[] = [
  {
    key: 'time_desc',
    label: '最近阅读',
    icon: 'time',
  },
  {
    key: 'time_asc',
    label: '最早阅读',
    icon: 'time-outline',
  },
  {
    key: 'progress_desc',
    label: '进度最高',
    icon: 'trending-up',
  },
  {
    key: 'progress_asc',
    label: '进度最低',
    icon: 'trending-down',
  },
]
